package com.jzy.gulimall.search.controller;

import com.jzy.common.exception.BizCodeEnum;
import com.jzy.common.to.es.SkuEsTo;
import com.jzy.common.utils.R;
import com.jzy.gulimall.search.service.ProductSaveService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * @author jzy
 * @date 2020/9/11 18:10
 */
@RestController
@RequestMapping("/search/save")
@Slf4j
public class ElasticSaveController {

    @Resource
    private ProductSaveService productSaveService;

    /**
     * spu管理 上架 更新es库
     */
    @PostMapping("/product")
    public R productStateUp(@RequestBody List<SkuEsTo> skuEsTos) {
        boolean aBoolean;
        try {
            aBoolean = productSaveService.productStateUp(skuEsTos);
        } catch (IOException e) {
            log.error("商品上架错误:{}", e.getMessage());
            return R.error(BizCodeEnum.PRODUCT_UP_EXCEPTION.getCode(), BizCodeEnum.PRODUCT_UP_EXCEPTION.getMessage());
        }
        if (!aBoolean) {
            return R.ok();
        } else {
            return R.error(BizCodeEnum.PRODUCT_UP_EXCEPTION.getCode(), BizCodeEnum.PRODUCT_UP_EXCEPTION.getMessage());
        }
    }
}
